<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Attribute otions control
 *
 * @see Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options
 */
?>
<div>
<ul class="messages">
    <li class="notice-msg">
        <ul>
            <li><?php echo Mage::helper('catalog')->__('If you do not specify an option value for a specific store view then the default (Admin) value will be used.') ?></li>
        </ul>
    </li>
</ul>
</div>

<div class="entity-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('catalog')->__('Manage Titles (Size, Color, etc.)') ?></h4>
    </div>
    <div class="box">
		    <div class="hor-scroll">
	        <table class="dynamic-grid" cellspacing="0" id="attribute-labels-table">
	            <tr>
	            <?php foreach ($this->getStores() as $_store): ?>
	                <th><?php echo $_store->getName() ?></th>
	            <?php endforeach; ?>
	            </tr>
	            <tr>
	                <?php $_labels = $this->getLabelValues() ?>
	                <?php foreach ($this->getStores() as $_store): ?>
	                <td>
	                    <input class="input-text<?php if($_store->getId()==0): ?> required-option<?php endif; ?>" type="text" name="frontend_label[<?php echo $_store->getId() ?>]" value="<?php echo $this->htmlEscape($_labels[$_store->getId()]) ?>" />
	                </td>
	                <?php endforeach; ?>
	            </tr>
	        </table>
		    </div>
    </div>
</div>
<br/>
<div class="entity-edit" id="matage-options-panel">
    <div class="entry-edit-head">
    <h4 class="icon-head head-edit-form fieldset-legend"><?php echo Mage::helper('catalog')->__('Manage Options (values of your attribute)') ?></h4>
    </div>
    <div class="box">
		<div class="hor-scroll">
	        <table class="dynamic-grid" cellspacing="0"  cellpadding="0">
	            <tr id="attribute-options-table">
		            <?php foreach ($this->getStores() as $_store): ?>
		                <th><?php echo $_store->getName() ?></th>
		            <?php endforeach; ?>
		                <th><?php echo Mage::helper('catalog')->__('Position') ?></th>
		                <th class="nobr a-center"><?php echo Mage::helper('catalog')->__('Is Default') ?></th>
		                <th><?php echo $this->getAddNewButtonHtml() ?></th>
		            </tr>
		    	    <tr class="no-display template" id="row-template">
		                <?php foreach ($this->getStores() as $_store): ?>
		                <td><input name="option[value][{{id}}][<?php echo $_store->getId() ?>]" value="{{store<?php echo $_store->getId() ?>}}" class="input-text<?php if($_store->getId()==0): ?> required-option<?php endif; ?>" type="text" /></td>
		                <?php endforeach; ?>
		                <td class="a-center"><input class="input-text" type="text" name="option[order][{{id}}]" value="{{sort_order}}" /></td>
		                <td><input class="input-radio" type="radio" name="default[]" value="{{id}}" /></td>
		                <td class="a-left">
		                    <input type="hidden" class="delete-flag" name="option[delete][{{id}}]" value="" />
		                    <?php echo $this->getDeleteButtonHtml() ?>
		                </td>
		            </tr>
	        </table>
		</div>
		<input type="hidden" id="option-count-check" value="" />
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
var optionDefaultInputType = 'radio';

// IE removes quotes from element.innerHTML whenever it thinks they're not needed, which breaks html.
var templateText =
        '<tr class="option-row">'+
<?php foreach ($this->getStores() as $_store): ?>
            '<td><input name="option[value][{{id}}][<?php echo $_store->getId() ?>]" value="{{store<?php echo $_store->getId() ?>}}" class="input-text<?php if($_store->getId()==0): ?> required-option<?php endif; ?>" type="text" /><\/td>'+
<?php endforeach; ?>
            '<td><input class="input-text" type="text" name="option[order][{{id}}]" value="{{sort_order}}"><\/td>'+
            '<td class="a-center"><input class="input-radio" type="{{intype}}" name="default[]" value="{{id}}" {{checked}} /><\/td>'+
            '<td class="a-left">'+
                '<input type="hidden" class="delete-flag" name="option[delete][{{id}}]" value="" />'+
                '<?php echo $this->getDeleteButtonHtml() ?>'+
            '<\/td>'+
        '<\/tr>';

var attributeOption = {
    table : $('attribute-options-table'),
    templateSyntax : /(^|.|\r|\n)({{(\w+)}})/,
    templateText : templateText,
    itemCount : 0,
    totalItems : 0,
    add : function(data) {
        this.template = new Template(this.templateText, this.templateSyntax);
        if(!data.id){
    		data = {};
    		data.id  = 'option_'+this.itemCount;
        }
        if (!data.intype)
            data.intype = optionDefaultInputType;

        Element.insert(this.table, {after: this.template.evaluate(data)});
        this.bindRemoveButtons();
        this.itemCount++;
        this.totalItems++;
        this.updateItemsCountField();
    },
    remove : function(event){
        var element = $(Event.findElement(event, 'tr')); // !!! Button already
                                                               // have table parent in safari
        // Safari workaround
        element.ancestors().each(function(parentItem){
           if (parentItem.hasClassName('option-row')) {
               element = parentItem;
               throw $break;
           } else if (parentItem.hasClassName('box')) {
               throw $break;
           }
        });


        if(element){
            var elementFlags = element.getElementsByClassName('delete-flag');
            if(elementFlags[0]){
                elementFlags[0].value=1;
            }

            element.addClassName('no-display');
            element.addClassName('template');
            element.hide();
            this.totalItems--;
            this.updateItemsCountField();
        }
    },
    updateItemsCountField: function() {
        if (this.totalItems > 0) {
            $('option-count-check').value = '1';
        } else {
            $('option-count-check').value = '';
        }
    },
    bindRemoveButtons : function(){
        var buttons = $$('.delete-option');
        for(var i=0;i<buttons.length;i++){
            if(!$(buttons[i]).binded){
                $(buttons[i]).binded = true;
                Event.observe(buttons[i], 'click', this.remove.bind(this));
            }
        }
    }

}
if($('row-template')){
    $('row-template').remove();
}
attributeOption.bindRemoveButtons();

if($('add_new_option_button')){
    Event.observe('add_new_option_button', 'click', attributeOption.add.bind(attributeOption));
}
Validation.addAllThese([
    ['required-option', '<?php echo Mage::helper('catalog')->__('Failed') ?>', function(v) {
        return !Validation.get('IsEmpty').test(v);
}]]);
Validation.addAllThese([
    ['required-options-count', '<?php echo Mage::helper('catalog')->__('Options is required') ?>', function(v) {
        return !Validation.get('IsEmpty').test(v);
}]]);
<?php foreach ($this->getOptionValues() as $_value): ?>
    attributeOption.add(<?php echo $_value->toJson() ?>);
<?php endforeach; ?>
//]]>
</script>